package com.hanhai.zrb.api.solr;

import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hanhai.zrb.api.biz.dao.mybatis.FundMapper;
import com.hanhai.zrb.api.biz.dao.mybatis.IntentionMapper;
import com.hanhai.zrb.api.biz.dao.mybatis.ProjectMapper;
import com.hanhai.zrb.model.fund.Fund;
import com.hanhai.zrb.model.intention.Intention;
import com.hanhai.zrb.model.project.ProjectSolrDto;

@Service("solrService")
public class SolrService {
	@Autowired
	private ProjectMapper projectMapper;
	@Autowired
	private IntentionMapper intentionMapper;
	@Autowired
	private FundMapper fundMapper;

	private String solrProjectUrl;

	private String solrIntentionUrl;
	private String solrFundUrl;

	public void synProject(long id) {
		ProjectSolrDto solrDto = projectMapper.selectSolrProjectSimple(id);
		SolrHelper<ProjectSolrDto> solrHelper = new SolrHelper<ProjectSolrDto>(
				solrProjectUrl);
		solrHelper.execUpdate(solrDto);
	}

	public void synIntention(Long id) {
		Intention intention = intentionMapper.selectByPrimaryKey(id);
		SolrHelper<Intention> solrHelper = new SolrHelper<Intention>(
				solrIntentionUrl);
		SolrInputDocument solrDoc = new SolrInputDocument();
		solrDoc.addField("id", intention.getId());
		solrDoc.addField("intro", intention.getIntro());
		solrDoc.addField("industry", intention.getIndustry());
		solrDoc.addField("district", intention.getDistrict());
		solrDoc.addField("amount", intention.getAmount());
		solrDoc.addField("mode", intention.getMode());
		solrDoc.addField("type", intention.getType());
		solrDoc.addField("userId", intention.getUserId());
		solrDoc.addField("pushTime", intention.getPushTime());
		solrDoc.addField("content", intention.getContent());
		solrDoc.addField("handlePerson", intention.getHandlePerson());
		solrDoc.addField("handleTime", intention.getHandleTime());
		solrDoc.addField("status", intention.getStatus());
		solrDoc.addField("note", intention.getNote());
		solrDoc.addField("projectId", intention.getProjectId());
		solrDoc.addField("auth", intention.getAuth());
		solrHelper.execUpdate(solrDoc);
	}

	public void synFund(Long id) {
		Fund fund = fundMapper.findById(id);
		SolrHelper<Fund> solrHelper = new SolrHelper<Fund>(solrFundUrl);
		SolrInputDocument solrDoc = new SolrInputDocument();
		solrDoc.addField("id", id);
		solrDoc.addField("titlesVal", fund.getTitlesVal());
		solrDoc.addField("fundNo", fund.getFundNo());
		solrDoc.addField("pushTime", fund.getPushTime());
		solrDoc.addField("updateTime", fund.getUpdateTime());
		solrDoc.addField("insertTime", fund.getInsertTime());
		solrDoc.addField("industryId", fund.getIndustryId());
		solrDoc.addField("total", fund.getTotal());
		solrDoc.addField("destination", fund.getDestination());
		solrDoc.addField("descrition", fund.getDescrition());
		solrDoc.addField("requirement", fund.getRequirement());
		solrDoc.addField("auth", fund.getAuth());
		solrDoc.addField("state", fund.getState());
		solrDoc.addField("userId", fund.getUserId());
		solrDoc.addField("investMode", fund.getInvestMode());
		solrDoc.addField("intention", fund.getIntention());
		solrDoc.addField("operatorId", fund.getOperatorId());
		solrDoc.addField("operatorTime", fund.getOperatorTime());
		solrDoc.addField("fristPushTime", fund.getFristPushTime());
		//solrDoc.addField("owner", "");
		solrHelper.execUpdate(solrDoc);
	}

	public void delFund(Long id) {
		SolrHelper<Intention> solrHelper = new SolrHelper<Intention>(
				solrFundUrl);
		solrHelper.execDelete("id", id.toString());
	}

	public String getSolrFundUrl() {
		return solrFundUrl;
	}

	public void setSolrFundUrl(String solrFundUrl) {
		this.solrFundUrl = solrFundUrl;
	}

	public String getSolrProjectUrl() {
		return solrProjectUrl;
	}

	public void setSolrProjectUrl(String solrProjectUrl) {
		this.solrProjectUrl = solrProjectUrl;
	}

	public String getSolrIntentionUrl() {
		return solrIntentionUrl;
	}

	public void setSolrIntentionUrl(String solrIntentionUrl) {
		this.solrIntentionUrl = solrIntentionUrl;
	}
}
